//
//  LGConsultRecordInpectCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/23.
//

#import "LGConsultRecordInpectCell.h"
#import "LGCheckPhotoTool.h"
@interface LGConsultRecordInpectCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIView *photoView;
/**   */
@property (nonatomic , strong)UIButton *countButton;
/**   */
@property (nonatomic , assign)CGFloat photoW;
/**   */
@property (nonatomic , strong)NSMutableArray *picArray;
/**   */
@property (nonatomic , strong)NSMutableArray *urlArray;

@end

@implementation LGConsultRecordInpectCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSInteger count = isPad?8:4;
        self.photoW = (CGFloat)(Screen_W-viewPix(63)-viewPix(8)*(count-1))/count;
        [self createSubView];
    }
    return  self;
}

-(void)setPhotoArray:(NSArray *)photoArray{
    _photoArray = photoArray;
    for (UIImageView *imageView in self.picArray) {
        [imageView removeFromSuperview];
    }
    [self.picArray removeAllObjects];
    
    self.photoView.hidden = (photoArray.count>0)?NO:YES;
    CGFloat marginTop = (photoArray.count>0)?viewPix(120):viewPix(10);
    [self.uploadBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(marginTop);
    }];
    self.urlArray = [NSMutableArray array];
    NSInteger maxCount = isPad?8:4;
    NSInteger count = (photoArray.count>maxCount)?maxCount:photoArray.count;
    for (NSInteger i=0; i<photoArray.count; i++) {
        NSString *urlStr = LGNSString(photoArray[i][@"url"]);
        [self.urlArray addObject:urlStr];
        if(i<count){
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.photoW+viewPix(8))*i, viewPix(40), self.photoW, self.photoW)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
            imageView.userInteractionEnabled = YES;
            imageView.cornerRidus = 3;
            imageView.tag = 1000+i;
            [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkBigPhotoAction:)]];
            [self.photoView addSubview:imageView];
            [self.picArray addObject:imageView];
        }
    }
    
    self.countButton.hidden = (photoArray.count>maxCount)?NO:YES;
    [self.countButton setTitle:[NSString stringWithFormat:@"+%ld",photoArray.count] forState:UIControlStateNormal];
    self.countButton.frame = CGRectMake((self.photoW+viewPix(8))*(maxCount-1), viewPix(40), self.photoW, self.photoW);
    [self.photoView bringSubviewToFront:self.countButton];
}

-(void)countButtonAction{
    LGCheckPhotoTool *bigPhotoView = [[LGCheckPhotoTool alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    bigPhotoView.title = @"图片报告";
    bigPhotoView.picArray = self.urlArray;
    [bigPhotoView showViewAnimation:CGPointMake(0, 0) index:0];
    [[UIApplication sharedApplication].keyWindow addSubview:bigPhotoView];
}

-(void)checkBigPhotoAction:(UITapGestureRecognizer *)sender{
    UIImageView *imageView = (UIImageView *)sender.view;
    NSInteger index = imageView.tag-1000;
    LGCheckPhotoTool *bigPhotoView = [[LGCheckPhotoTool alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    bigPhotoView.title = @"图片报告";
    bigPhotoView.picArray = self.urlArray;
    [bigPhotoView showViewAnimation:CGPointMake(0, 0) index:index];
    [[UIApplication sharedApplication].keyWindow addSubview:bigPhotoView];
}

-(void)checkPhotoAction{
    if(self.checkMorePhotoAction){
        self.checkMorePhotoAction();
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.baseView];
    [self.contentView addSubview:self.photoView];
    [self.contentView addSubview:self.uploadBtn];
    [self.contentView addSubview:self.addButton];
    [self.photoView addSubview:self.countButton];
    [self createTitleView];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.contentView).offset(viewPix(16));
        make.top.right.equalTo(self.contentView).offset(-viewPix(16));
    }];
    [self.uploadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(5));
        make.left.equalTo(self.contentView).offset(viewPix(28));
        make.right.equalTo(self.contentView).offset(-viewPix(28));
        make.height.equalTo(@(viewPix(32)));
    }];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.uploadBtn.mas_bottom).offset(viewPix(12));
        make.left.right.height.equalTo(self.uploadBtn);
        make.bottom.equalTo(self.contentView).offset(-viewPix(12));
    }];
}
-(NSMutableArray *)picArray{
    if(!_picArray){
        _picArray = [NSMutableArray array];
    }
    return _picArray;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}
-(UIView *)photoView{
    if(!_photoView){
        _photoView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(28), 0, Screen_W-viewPix(56), viewPix(120))];
        _photoView.hidden = YES;
    }
    return _photoView;
}

-(UIButton *)countButton{
    if(!_countButton){
        _countButton = [UIButton buttonWithTitle:@"#FFFFFF" titleFont:LGFontWeight(22, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _countButton.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
        _countButton.cornerRidus = 3;
        [_countButton addTarget:self action:@selector(countButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _countButton;
}

-(void)createTitleView{
    UILabel *titleLabel = [UILabel lableWithText:@"图片报告" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    titleLabel.frame = CGRectMake(0, viewPix(12), viewPix(90), viewPix(20));
    [self.photoView addSubview:titleLabel];
    
    UIButton *checkBtn = [UIButton buttonWithTitle:@"查看" titleFont:LGFont(14) textColor:@"#666666" imageName:@"grayTip12"];
    checkBtn.frame = CGRectMake(Screen_W-viewPix(56)-viewPix(60), viewPix(7), viewPix(60), viewPix(30));
    [checkBtn addTarget:self action:@selector(checkPhotoAction) forControlEvents:UIControlEventTouchUpInside];
    [checkBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:1];
    [self.photoView addSubview:checkBtn];
}

-(UIButton *)uploadBtn{
    if(!_uploadBtn){
        _uploadBtn = [UIButton buttonWithTitle:@"上传报告" titleFont:LGFont(15) textColor:@"#3291FF" imageName:@""];
        _uploadBtn.layer.borderColor = [UIColor colorWithString:@"#3291FF"].CGColor;
        _uploadBtn.layer.borderWidth = 1;
        _uploadBtn.cornerRidus = viewPix(16);
    }
    return _uploadBtn;
}

-(UIButton *)addButton{
    if(!_addButton){
        _addButton = [UIButton buttonWithTitle:@"添加核心指标" titleFont:LGFont(15) textColor:@"#3291FF" imageName:@""];
        _addButton.layer.borderColor = [UIColor colorWithString:@"#3291FF"].CGColor;
        _addButton.layer.borderWidth = 1;
        _addButton.cornerRidus = viewPix(16);
    }
    return _addButton;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
